package com.oracle.third.service.email.listener;

import com.alibaba.fastjson.JSONObject;
import com.oracle.third.service.email.listener.service.EmailService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EmailQueueListener implements ChannelAwareMessageListener {

    @Autowired
    private EmailService emailService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        //拉取消息
        String email = new String(message.getBody());
        email = (String) JSONObject.parse(email);
        //调用service消费消息，发送邮件验证码
        try {
            this.emailService.sendEmailRandom(email);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            e.printStackTrace();
        }
    }
}
